<?php

class CursoController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $model = new Cursos();
        $grid = $this->_helper->BvbGrid->grid($model,'grid_cursos');
        $grid->setGridColumns(array('protocolo','curso','area_id','servidores','relator_id','status_id','obs','dt_recebimento','dt_entrega'));
        $grid->updateColumn('protocolo', array('position'=>'1')); 
        $grid->updateColumn('curso', array('position'=>'2')); 
        $grid->updateColumn('area_id', array('title'=>'Área','position'=>'4')); 
        $grid->updateColumn('status_id', array('title'=>'Status')); 
        $grid->updateColumn('relator_id', array('title'=>'Relator')); 
        $grid->updateColumn('dt_recebimento',array(
            'format'=>array('date',array('date_format'=>"dd/MM/Y")),
            'search'=>false,
            'title'=>'Receb.'));
        $grid->updateColumn('dt_entrega',array(
            'format'=>array('date',array('date_format'=>"dd/MM/Y")),
            'search'=>false,
            'title'=>'Entrega'));
        
        $grid->setNumberRecordsPerPage(10); 
        $grid->setShowFiltersInExport(true); 

        $grid->listenEvent('crud.before_insert',array($this,'converterDatetoMysql'));
        $grid->listenEvent('crud.before_update',array($this,'converterDatetoMysql'));
        
        
        $data = $grid->getForm(1)->getElement('dt_recebimento')->getValue();
        $grid->getForm(1)->getElement('dt_recebimento')->setValue($this->converterDatefromMysql($data));

        $data = $grid->getForm(1)->getElement('dt_entrega')->getValue();
        $grid->getForm(1)->getElement('dt_entrega')->setValue($this->converterDatefromMysql($data));
        

        $filters = new Bvb_Grid_Filters(); 
        $filters->addFilter('protocolo',array('render'=>'text')); 
        $filters->addFilter('curso', array('distinct' => array('field' => 'curso', 'name' => 'curso','order'=>'curso ASC'))); 
        $filters->addFilter('curso',array('render'=>'text')); 
        $filters->addFilter('servidor_id', array('distinct' => array('field' => 'servidor_id', 'name' => 'servidor_id','order'=>'nome ASC'))); 
        $filters->addFilter('relator_id', array('distinct' => array('field' => 'relator_id', 'name' => 'relator_id','order'=>'nome ASC'))); 
        $filters->addFilter('status_id', array('distinct' => array('field' => 'status_id', 'name' => 'status_id'))); 
        $filters->addFilter('obs',array('render'=>'text')); 
        $grid->addFilters($filters); 
        
        $this->view->pages = $grid->deploy();
    }     
    
    function converterDatetoMysql ($event)
    {
        $newValues = $event->getParam('newValues');
        $data = $newValues['dt_recebimento'];
        $newValues['dt_recebimento'] = implode("-",array_reverse(explode("/",$data)));
        $data = $newValues['dt_entrega'];
        $newValues['dt_entrega'] = implode("-",array_reverse(explode("/",$data)));
        $event->setParam('newValues',$newValues);
    }
        
    function converterDatefromMysql ($data)
    {
        return implode("/",array_reverse(explode("-",$data)));
    }
        
}

